//=====================================================================
//						<网络API>
//
// 本文件维护所有通信接口
//=====================================================================

//登录
function apiLogin(mobile,password){
	var data	= {"loginName":mobile,"loginPass":password};
	_Api(URI_PASSPORT,"passport/login",data,function(result){
	//Api("passport/login",data,function(result){
		_TR(result);
		setTimeout(function(){
			redirectUrl("index.html");
		},100);
	});
}

//登录
function apiLoginByToken(token){
	var data	= {"token":token};
	Api("passport/login",data,function(result){
		setTimeout(function(){
			redirectUrl("index.html");
		},100);
	});
}

//退出登录
function apiLogout(cb){
	Api("passport/logout",null,function(){
//		setTimeout(function(){
//			redirectUrl("login.html");
//		},100);
		if(cb)cb();
	});
}

//登录
function apiLoginData(){
	Api("boss/getInfo",null,function(data){
		_TR(".......");
		if(null==GUser)GUser={};
		doInfo("登陆",data.baseinfo.realName + ",欢迎回来!");
		GUser["operator"]	= data.baseinfo;
		GUser["menu"]		= data.menu;
		EMIT("user.menu",data.menu);
		EMIT("user.data",data.baseinfo);
		_TR("登陆");
	});
}


//====================================================================================
//								权限管理
//====================================================================================

function apiListOperator(options,cb){
	Api("boss/pms/operator/list",options,function(result){
		if(cb)cb(result.operators,result.page);
	});
}
function apiFindOperator(options,cb){
	Api("boss/pms/operator/find",options,function(result){
		if(cb)cb(result.operator,result.dictRoles,result.roleIds);
	});
}
function apiAddOperator(options,cb){
	Api("boss/pms/operator/add",options,function(result){
		if(cb)cb();
	});
}
function apiEditOperator(options,cb){
	Api("boss/pms/operator/edit",options,function(result){
		if(cb)cb();
	});
}


function apiListPermission(options,cb){
	Api("boss/pms/permission/list",options,function(result){
		if(cb)cb(result.permissions,result.page);
	});
}
function apiFindPermission(options,cb){
	Api("boss/pms/permission/find",options,function(result){
		if(cb)cb(result.permission,result.dictRoles,result.roleIds);
	});
}
function apiAddPermission(options,cb){
	Api("boss/pms/permission/add",options,function(result){
		if(cb)cb();
	});
}
function apiEditPermission(options,cb){
	Api("boss/pms/permission/edit",options,function(result){
		if(cb)cb();
	});
}


function apiListRole(options,cb){
	Api("boss/pms/role/list",options,function(result){
		if(cb)cb(result.roles,result.page);
	});
}
function apiFindRole(options,cb){
	Api("boss/pms/role/find",options,function(result){
		if(cb)cb(result.role,result.dictPms,result.pmsIds,result.dictMenu,result.menuIds);
	});
}
function apiAddRole(options,cb){
	Api("boss/pms/role/add",options,function(result){
		if(cb)cb();
	});
}
function apiEditRole(options,cb){
	Api("boss/pms/role/edit",options,function(result){
		if(cb)cb();
	});
}


function apiListMenu(options,cb){
	Api("boss/pms/menu/list",options,function(result){
		if(cb)cb(result.menus,result.page);
	});
}
function apiFindMenu(options,cb){
	Api("boss/pms/menu/find",options,function(result){
		if(cb)cb(result.menu);
	});
}
function apiAddMenu(options,cb){
	Api("boss/pms/menu/add",options,function(result){
		if(cb)cb(result.menu);
	});
}
function apiEditMenu(options,cb){
	Api("boss/pms/menu/edit",options,function(result){
		if(cb)cb(result.menu);
	});
}
function apiDelMenu(options,cb){
	Api("boss/pms/menu/del",options,cb);
}

//====================================================================================
//								商户管理
//====================================================================================
//--- 品牌相关
function apiListBrand(options,cb){
	Api("boss/mch/brand/list",options,function(result){
		if(cb)cb(result.brands,result.page);
	});
}
function apiListAllBrand(options,cb){
	Api("boss/mch/brand/listAll",options,cb);
}
function apiFindBrand(options,cb){
	Api("boss/mch/brand/find",options,cb);
}
function apiAddBrand(options,cb){
	Api("boss/mch/brand/add",options,cb);
}
function apiEditBrand(options,cb){
	Api("boss/mch/brand/edit",options,cb);
}
function apiDeleteBrand(options,cb){
	Api("boss/mch/brand/del",options,cb);
}


function apiListSeries(options,cb){
	Api("boss/mch/brand/series/list",options,function(result){
		if(cb)cb(result.series,result.page);
	});
}
function apiListAllSeries(options,cb){
	Api("boss/mch/brand/series/listAll",options,cb);
}
function apiFindSeries(options,cb){
	Api("boss/mch/brand/series/find",options,cb);
}
function apiAddSeries(options,cb){
	Api("boss/mch/brand/series/add",options,cb);
}
function apiEditSeries(options,cb){
	Api("boss/mch/brand/series/edit",options,cb);
}
function apiDeleteSeries(options,cb){
	Api("boss/mch/brand/series/del",options,cb);
}


function apiListUser(options,cb){
	Api("boss/mch/user/list",options,function(result){
		if(cb)cb(result.users,result.page);
	});
}
function apiListAllUser(options,cb){
	Api("boss/mch/user/listAll",options,cb);
}
function apiFindUser(options,cb){
	Api("boss/mch/user/find",options,cb);
}
function apiAddUser(options,cb){
	Api("boss/mch/user/add",options,cb);
}
function apiEditUser(options,cb){
	Api("boss/mch/user/edit",options,cb);
}
function apiDeleteUser(options,cb){
	Api("boss/mch/user/del",options,cb);
}



//--- 品牌经销商\经销商\门店相关
function apiListDealer(options,cb){
	Api("boss/mch/dealer/list",options,function(result){
		if(cb)cb(result.dealers,result.page);
	});
}
function apiListAllDealer(options,cb){
	Api("boss/mch/dealer/listAll",options,cb);
}
function apiFindDealer(options,cb){
	Api("boss/mch/dealer/find",options,cb);
}
function apiAddDealer(options,cb){
	Api("boss/mch/dealer/add",options,cb);
}
function apiEditDealer(options,cb){
	Api("boss/mch/dealer/edit",options,cb);
}
function apiDeleteDealer(options,cb){
	Api("boss/mch/dealer/del",options,cb);
}

function apiListMerchant(options,cb){
	Api("boss/mch/merchant/list",options,function(result){
		if(cb)cb(result.merchants,result.page);
	});
}
function apiListMerchantName(options,cb){
	Api("boss/mch/merchant/listName",options,cb);
}
function apiFindMerchant(options,cb){
	Api("boss/mch/merchant/find",options,cb);
}
function apiAddMerchant(options,cb){
	Api("boss/mch/merchant/add",options,cb);
}
function apiEditMerchant(options,cb){
	Api("boss/mch/merchant/edit",options,cb);
}
function apiDeleteMerchant(options,cb){
	Api("boss/mch/merchant/del",options,cb);
}

//function apiListMerchant(options,cb){
//	Api("boss/mch/merchant/list",options,function(result){
//		if(cb)cb(result.merchants,result.page);
//	});
//}
//function apiFindMerchant(options,cb){
//	Api("boss/mch/merchant/find",options,function(result){
//		if(cb)cb(result.merchant);
//	});
//}
function apiAddEmployee(options,cb){
	Api("boss/mch/user/add",options,function(result){
		if(cb)cb();
	});
}
//function apiEditMerchant(options,cb){
//	Api("boss/mch/merchant/edit",options,function(result){
//		if(cb)cb();
//	});
//}
//function apiDeleteMerchant(options,cb){
//	Api("boss/mch/merchant/del",options,function(result){
//		if(cb)cb();
//	});
//}
//====================================================================================
//								字典
//====================================================================================
function apiListChinaCode(options,cb){
	Api("boss/dict/china/list",options,function(result){
		if(cb)cb(result.areas,result.page);
	});
}

function apiExportChinaCode(options,cb){
	window.open(URI_BASE+"boss/dict/china/export");
}

function apiListStyle(options,cb){
	Api("boss/dict/style/list",options,function(result){
		if(cb)cb(result.styles,result.page);
	});
}

//====================================================================================
//								户型设计
//====================================================================================
function apiListCommunity(options,cb){
	Api("boss/dsn/community/list",options,function(result){
		if(cb)cb(result.communitys,result.page);
	});
}

function apiImportCommunity(options,cb){
	ApiPost("boss/dsn/community/list",options,function(result){
		if(cb)cb(result);
	});
}

function apiListHouseDiagram(options,cb){
	Api("boss/dsn/house/list",options,function(result){
		if(cb)cb(result.diagrams,result.page);
	});
}

function apiFindHouseDiagram(options,cb){
	Api("boss/dsn/house/find",options,function(result){
		if(cb)cb(result);
	});
}


//====================================================================================
//								统计
//====================================================================================
function apiStatQueryCount(options,cb){
	Api("stat/queryCount",options,function(result){
		if(cb)cb(result);
	});
}


//====================================================================================
//								品类相关
//====================================================================================

//--- 品类
function apiListCategory(options,cb){
	Api("boss/dict/category/list",options,function(result){
		if(cb)cb(result.categorys,result.page);
	});
}

function apiListCategoryAll(options,cb){
	Api("boss/dict/category/listAll",options,function(result){
		if(cb)cb(result);
	});
}

function apiFindCategory(options,cb){
	Api("boss/dict/category/find",options,function(result){
		if(cb)cb(result);
	});
}

function apiListSubCategory(options,cb){
	Api("boss/dict/category/listChild",options,function(result){
		if(cb)cb(result);
	});
}

function apiListCategoryAttribute(options,cb){
	Api("boss/dict/category/listAttribute",options,function(result){
		if(cb)cb(result);
	});
}

function apiListCategoryAttributeDef(options,cb){
	Api("boss/dict/category/listAttributeDef",null,function(result){
		if(cb)cb(result);
	});
}

function apiEditCategory(options,cb){
	// 数据可能很大
	ApiPost("boss/dict/category/edit",options,function(result){
		if(cb)cb(result);
	});
}

//--- 产品风格
function apiListProdStyle(options,cb){
	Api("boss/prod/style/listAll",options,function(result){
		if(cb)cb(result.styles);
	});
}

//--- 列出属性
function apiListProdAttribute(options,cb){
	Api("boss/prod/attr/list",options,function(r){
		if(cb)cb(r.attributes);
	});
}

//--- 列出属性的所有值
function apiListProdAttributeWithValue(options,cb){
	Api("boss/prod/attr/listByCategory",options,function(r){
		if(cb)cb(r);
	});
}

//====================================================================================
//								产品相关
//====================================================================================

//--- [spu]查询
function apiListProduct(options,cb){
	Api("boss/prod/list",options,function(r){
		if(cb)cb(r.products,r.page);
	});
}

function apiFindProduct(options,cb){
	Api("boss/prod/find",options,function(r){
		if(cb)cb(r);
	});
}
//---  购物车
function apiListCart(option,cb) {
	Api("crm/crmproduct/listCart",options,cb);
}

function apiEditProduct(options,cb){
	Api("boss/prod/editSpu",options,function(r){
		if(cb)cb(r);
	});
}

//--- [sku]查询
function apiListProductSku(options,cb){
	Api("boss/prod/listSku",options,function(r){
		if(cb)cb(r.attributes);
	});
}


//--- [属性值]查询
function apiListProductByAttributeV(options,cb){
	Api("boss/prod/listByAttributeV",options,function(r){
		if(cb)cb(r.attributes);
	});
}

//--- 通过属性查询
function apiListProductByAttribute(options,cb){
	Api("boss/prod/listByAttribute",options,function(r){
		if(cb)cb(r.attributes);
	});
}



//function apiFindMerchant(options,cb){
//	Api("boss/shop/category/find",options,function(result){
//		if(cb)cb(result.category);
//	});
//}
//function apiAddMerchant(options,cb){
//	Api("boss/shop/category/add",options,function(result){
//		if(cb)cb();
//	});
//}
//function apiEditMerchant(options,cb){
//	Api("boss/shop/category/edit",options,function(result){
//		if(cb)cb();
//	});
//}
//function apiDeleteMerchant(options,cb){
//	Api("boss/shop/category/del",options,function(result){
//		if(cb)cb();
//	});
//}


